/**
 * 购买产品
 */
define(['ldfw/core', 'art', 'weui','text!tpl/product/purchase.tpl','comps/session',
        'comps/ajax', 'ldfw/ldlibs/dict','ldfw/utils/format', 'ldfw/helper/formatHelper','jquery-weui'],
    function (LDFW,art,weui,tpl,session,ajax,dict) {
        var View = LDFW.Container.extend({
            className: 'panel',
            tpl: tpl,
            events:{
                'click [operate="buy"]':'buyProduct',
                'change #purchaseAgree':'togglePurchaseAgree'
            },
            initialize:function(){
                this.on('shown',this.loadData)
                this.firstBuy = true
            },
            loadData:function(){
                const _this = this
                //查询产品详情
                ajax.post('pfL.1.217',{pd_no:this.pd_no}).success(function(resp){
                    let prdocutInfo = resp.result
                    _this.model.set(prdocutInfo)
                })
                //查询是否已买过
                ajax.post('pfL.1.205',{
                    pd_no:this.pd_no
                }).success(function(resp){
                    if(resp.result.length){
                        _this.firstBuy = false
                    }
                })
            },
            compAddedHandler:function(comp){
                this.amtComp = comp
            },
            setProductNo:function(pd_no){
                this.pd_no = pd_no
            },
            disablePurchaseBtn:function(flag){
                if(flag){
                    this.$el.find('[operate="buy"]').addClass('weui-btn_disabled').attr('disabled','disabled')
                }else{
                    this.$el.find('[operate="buy"]').removeClass('weui-btn_disabled').removeAttr('disabled')
                }
            },
            togglePurchaseAgree:function(event){
                if(event.target.checked) {
                    this.disablePurchaseBtn(false)
                }else{
                    this.disablePurchaseBtn(true)
                }
            },
            buyProduct:function(){
                const _this = this
                weui.form.validate('#buyForm',function(error) {
                    if(error) {
                        $(error.ele).addClass('error')
                        $(error.ele).one('blur', function () {
                            $(this).removeClass('error')
                        })
                    }else{
                        let buyAmt =_this.amtComp.getValue(),
                            minAmt = _this.model.get('min_amt'),
                            raise_status = _this.model.get('raise_status'), //募集状态
                            busi_oper = 2   //业务操作
                        if(_this.firstBuy && buyAmt < minAmt){
                            $.alert('对不起，您的金额小于最低投资金额：'+format('number','$.00',minAmt))
                        }else{
                            if(raise_status==1){
                                busi_oper = 1
                            }else if(raise_status==3){
                                busi_oper =2
                            }
                            //发起申请
                            ajax.post('pfL.1.216',{
                                busi_oper:busi_oper,
                                pd_no:_this.pd_no,
                                occur_amt:buyAmt,
                                mismatch_warning_file:session.getItem('mismatch_warning_file'),
                                risk_warning_file:session.getItem('risk_warning_file'),
                                product_apply_timestamp:session.getItem('time_stamp')
                            }).success(function(resp){
                                let busi_oper_str = dict.getDict('busi_oper',busi_oper)
                                $.toast(busi_oper_str+'成功',1000,function(){
                                    LDFW.navigate('main')
                                })
                            })
                        }
                    }
                })
            }
        })
        return new View()
    })
